
package com.siqi.hr.system.custom.dao.mysql;

import com.siqi.hr.common.Constants;
import com.siqi.hr.common.redis.RedisAPI;
import com.siqi.hr.common.utils.Const;
import com.siqi.hr.common.utils.TableConstant;
import com.siqi.hr.system.common.enums.FieldTypeEnum;
import com.siqi.hr.system.common.model.ExtraParameter;
import com.siqi.hr.system.common.model.Module;
import com.siqi.hr.system.common.model.ModuleType;
import com.siqi.hr.system.common.sql.SField;
import com.siqi.hr.system.custom.dao.intf.FieldEmployeeDao;
import com.siqi.hr.system.custom.dao.intf.FieldRecordDao;
import com.siqi.hr.system.custom.entity.FieldRecord;
import com.siqi.hr.system.organization.dao.intf.RoleDao;
import com.siqi.hr.system.organization.entity.Employee;
import com.siqi.hr.system.common.enums.FieldPosition;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 处理员工与字段布局、过滤条件布局关系通用dao实现类层
 * @author    xiaogq
 * @date         2016/8/24.
 */
@Repository("fieldEmployeeDao")
public class FieldEmployeeImpl implements FieldEmployeeDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private FieldRecordDao fieldRecordDao;


	/**
	 * 查询当前登录员工设置的查询条件字段通用方法
	 * @author                   xiaogq
	 * @param extraParam         封装过滤条件相关对象信息
	 */
	public String queryEmployeeFilter(ExtraParameter extraParam)throws Exception{
		//查询过滤条件显示字段
		List<FieldRecord> fieldList = queryEntityLayoutField(extraParam.getEmployee(), extraParam.getEntityId(), FieldPosition.Filter);
		//设置过滤条件显示布局
		StringBuilder table = new StringBuilder();
		table.append("<tr>");
		for (int i = 0; i < fieldList.size(); i++) {
			if(i!=0 && i%2 == 0){
				table.append("</tr><tr>");
			}
			//获取字段信息
			FieldRecord field = fieldList.get(i);
			//设置字段信息到过滤条件对象
			extraParam.setFieldRecord(field);
			//获得条件组件
			Map<String, Object> condMap = getFilterComponent(extraParam);

			//字段名 class="filter-field" title="名称"
			table.append("<td name='field-"+condMap.get("fieldName")+"' class='filter-field'>"+condMap.get("localization")+"&nbsp;&nbsp;</td>");
			//条件
			table.append("<td class='filter-cond'>"+condMap.get("condition").toString()+"</td>");
			//值
			table.append("<td class='filter-value'>"+condMap.get("inputHtml").toString()+"</td>");

			if(fieldList.size() == 1){
				table.append("<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>");
			}
		}
		table.append("</tr>");

		if(table.toString().indexOf("td")<0){
			table = new StringBuilder().append("<tr class='nocondition'><td style='width:100%;font-size:24px;color:#c0dadf;text-align:center;font-family:微软雅黑;'>没有过滤条件</td></tr>");
		}

		return table.toString();
	}

	/**
	 * 查询实体列表布局或者过滤条件布局显示字段通用方法
	 * @param employee            当前登录系统的用户id
	 * @param entityId            实体id
	 * @param position            查询列表布局或过滤条件布局标识(List:列表布局，Filter：过滤条件布局)
	 * @return
	 */
	public List<FieldRecord> queryEntityLayoutField(Employee employee,Integer entityId,FieldPosition position)throws Exception{
		String empId = employee.getId();
		List<FieldRecord> fieldList = new ArrayList<FieldRecord>();
		//先从前台设置布局、过滤条件的缓存中取布局、过滤条件字段
		if (employee != null) {
			fieldList = (List<FieldRecord>) RedisAPI.getKeyObject(entityId+"_"+empId+"_"+position.name(), Constants.DEFAULTDB_INDEX);
		}
        //如果前台没有设置布局、过滤条件字段则从后台设置的布局、过滤条件字段
		if ((fieldList == null) || (fieldList.size() == 0)) {
			fieldList = (List<FieldRecord>) RedisAPI.getKeyObject(entityId+"_"+position.name(), Constants.DEFAULTDB_INDEX);
		}
		//缓存没有则查询数据库
		if( null == fieldList || fieldList.size() == 0 ){
			//查询用户设置的实体显示字段
			if( null != employee ){
				fieldList = queryEmpSetEntityLayoutField(empId, entityId, position);
				RedisAPI.setKeyObject(entityId+"_"+empId+"_"+position.name(),fieldList,Constants.DEFAULTDB_INDEX);
			}
			//用户没有设置则获取系统默认设置
			if(null == fieldList || fieldList.size() == 0){
				//获取缓存中实体默认的显示字段
				fieldList = querySysSetEntityLayoutField(entityId, position);
				RedisAPI.setKeyObject(entityId+"_"+position.name(),fieldList,Constants.DEFAULTDB_INDEX);
			}
		}
		//过滤部分实体不显示引用实体字段的字段 及 验证字段级权限
		if( null != employee && fieldList != null && fieldList.size() > 0 ){
			List<FieldRecord> noAuthList = new ArrayList<FieldRecord>();
			Map<String,Boolean> authMap = roleDao.getRoleEntityFieldListAuth(employee.getRole().getId(), entityId);
			for( FieldRecord fr : fieldList ){
				//验证字段级权限
				if( null != authMap.get(fr.getName()) && authMap.get(fr.getName()) ){
					noAuthList.add(fr);
				}
			}
			fieldList.removeAll(noAuthList);
		}

		return fieldList;

	}

	/**
	 * 获取过滤条件与条件值
	 * @param extraParam     过滤条件对象信息
	 * @return
	 */
	public Map<String, Object> getFilterComponent(ExtraParameter extraParam) throws Exception{
		String fieldName = extraParam.getFieldRecord().getName();	//完整的字段名 pk_t_3_1___name
		String queryName = extraParam.getFieldRecord().getName();	//作为查询条件的字段名name
		String localization = "";
		if( StringUtils.isNotEmpty(extraParam.getFieldRecord().getInchinese()) ){
			localization = extraParam.getFieldRecord().getInchinese();
		}
		String domid = extraParam.getTableName() + "__" + fieldName;

		Map<String, Object> condMap = new HashMap<String, Object>();
		if(extraParam.getEntityId() == TableConstant.ROLE_TCODE && queryName.equals("loginModule_varchar")){ //如果是角色实体中的可登陆模块
			StringBuffer tempCond = new StringBuffer();
			tempCond.append("<select class='filter-select form-control' id='conditon-" + fieldName + "' >");
			tempCond.append("<option value='exactlike'>包含</option>");
			tempCond.append("<option value='exactnotlike'>不包含</option>");
			tempCond.append("<option value='modulenull'>为空</option>");
			tempCond.append("<option value='modulenotnull'>不为空</option>");
			tempCond.append("</select>").toString();
			condMap.put("condition", tempCond.toString());

			StringBuffer tempInpHtml = new StringBuffer();
			List<Module> moduleList = ModuleType.getAllModule();
			tempInpHtml.append("<select id='"+domid+"' class='filter-select form-control' ><option value='' ></option>");
			for(Module mod : moduleList){
				tempInpHtml.append("<option value='"+mod.getCode()+"'>"+mod.getInchinese()+"</option>");
			}
			tempInpHtml.append("</select>");
			condMap.put("inputHtml", tempInpHtml.toString());
		}else{
			if(queryName.startsWith(SField.PK_FIELD_BEGIN_WITH.toLowerCase())){ //外键
				if(extraParam.isTable()){
					condMap = getFilterCondition(fieldName, extraParam, FieldTypeEnum.FOREIGNKEYTABLE);
				}else {
					condMap = getFilterCondition(fieldName, extraParam, FieldTypeEnum.FOREIGNKEY);
				}
			}else{ //非外键
				if(queryName.endsWith("_date") || queryName.endsWith("_timestamp") || queryName.endsWith("_datetime")){ //时间类型
					condMap = getFilterCondition(fieldName, extraParam, FieldTypeEnum.TIME);
				}else if(queryName.endsWith("_bit")) { //布尔类型
					condMap = getFilterCondition(fieldName,extraParam, FieldTypeEnum.BIT);
				}else if(queryName.endsWith("_radio") || queryName.endsWith("_singlebox") ) { //下拉框类型
					Integer quoteEntityId = extraParam.getEntityId();
					List<Map<String, Object>> selectList = this.queryEntityFieldSelectBox(quoteEntityId, extraParam.getFieldRecord());
					extraParam.setSelectList(selectList);
					condMap = getFilterCondition(fieldName, extraParam, FieldTypeEnum.SELECTBOX);
				}else if( queryName.endsWith("_mutibox") ){//下拉多选（肖功钦）
					Integer quoteEntityId = extraParam.getEntityId();
					List<Map<String, Object>> selectList = this.queryEntityFieldSelectBox(quoteEntityId, extraParam.getFieldRecord());
					extraParam.setSelectList(selectList);
					condMap = getFilterCondition(fieldName, extraParam, FieldTypeEnum.MUTIBOX);
				}else if(queryName.endsWith("_int") || queryName.endsWith("_long")
						|| fieldName.endsWith("_decimal") || fieldName.endsWith("_precent")
						|| fieldName.endsWith("_currency")) { //数值相关类型
					condMap = getFilterCondition(fieldName, extraParam, FieldTypeEnum.INT);
				}else { //其他
					condMap = getFilterCondition(fieldName, extraParam, FieldTypeEnum.VARCHAR);
				}
			}
		}
		condMap.put("fieldName", fieldName);
		condMap.put("localization", localization);
		return condMap;
	}

	public Map<String, Object> getFilterCondition(String fieldName, ExtraParameter exParam, FieldTypeEnum fieldtype){
		String domid = exParam.getTableName() + "__" + fieldName;
		//条件
		StringBuilder condition = new StringBuilder().append("<select class='filter-select form-control' id='conditon-"+fieldName+"' " + " onchange='appendFilterInputBox(this)' >");
		//输入框
		StringBuilder inputHtml = new StringBuilder();
		switch (fieldtype) {
			case FOREIGNKEY: {
				condition.append("<option value='eq'>=</option>");
				condition.append("<option value='noteq'>不等于</option>");
				condition.append("<option value='isnull'>为空</option>");
				condition.append("<option value='notnull'>不为空</option>");

				String stampScope = "searchFilter";
				inputHtml.append("<span class='searchSpan'>" +
						"<span class='searchBox'>" +
						"<input type='hidden' id='" + domid + "' name='" + fieldName + "' class='form-control'/>" +
						"<input type='text' id='" + domid + "_osv' readonly='readonly' onclick='parent.filterQuoteClick(this,null)' class='searchInput filter-select form-control' " + "valueinputid='" + domid + "' showinputid='" + domid + "_osv'/>" +
						"<span id='" + domid + "_span' class='filter-search glyphicon glyphicon-search' " + " valueinputid='" + domid + "' showinputid='" + domid + "_osv'></span>" +
						"</span>" +
						"</span>"
				);
				break;
			}
			case FOREIGNKEYTABLE: {
				condition.append("<option value='eq'>=</option>");
				condition.append("<option value='noteq'>不等于</option>");
				condition.append("<option value='isnull'>为空</option>");
				condition.append("<option value='notnull'>不为空</option>");

				String stampScope = "searchFilter";
				inputHtml.append("<span class='searchSpan'>" +
						"<span class='searchBox'>" +
						"<input type='text' id='" + domid + "_osv' class='searchInput filter-select form-control' " + "valueinputid='" + domid + "' showinputid='" + domid + "_osv'/>" +
						"</span>" +
						"</span>"
				);
				break;
			}
			case TIME: {
				condition.append("<option value='gt'>晚于</option>");
				condition.append("<option value='lt'>早于</option>");
				condition.append("<option value='between'>区间</option>");
				condition.append("<option value='isnull'>为空</option>");
				condition.append("<option value='notnull'>不为空</option>");

				inputHtml.append("<span id='inputspan-" + fieldName + "'>" +
						"<input type='text' id='" + domid + "' class='filter-input filter-select form-control' />" +
						"<input type='text' id='"+domid+"-r' class='filter-input2 filter-select form-control' style='display:none;' />" +
						"</span>");
				break;
			}
			case DECIMAL: {
				condition.append("<option value='between'>区间</option>");
				condition.append("<option value='gt'>&gt;</option>");
				condition.append("<option value='ge'>&gt;=</option>");
				condition.append("<option value='lt'>&lt;</option>");
				condition.append("<option value='le'>&lt;=</option>");
				condition.append("<option value='noteq'>&lt;&gt;</option>");
				inputHtml.append("<span id='inputspan-" + fieldName + "'>" +
						"<input type='text' id='" + domid + "' size='10' class='filter-input1 filter-select form-control '/>" +
						"<input type='text' id='"+domid+"-r' size='10' class='filter-input2 filter-select form-control' />"+
						"</span>");
				break;
			}
			case VARCHAR: {
				condition.append("<option value='include'>包含</option>");
				condition.append("<option value='notinclude'>不包含</option>");
				condition.append("<option value='beginwith'>开头是</option>");
				condition.append("<option value='endwith'>结尾是</option>");
				condition.append("<option value='isnull'>为空</option>");
				condition.append("<option value='notnull'>不为空</option>");
				condition.append("<option value='eq'>=</option>");
				inputHtml.append("<input type='text' id='" + domid + "' class='filter-input filter-select form-control'/>");
				break;
			}
			case BIT: {
				condition.append("<option value='eq'>=</option>");

				inputHtml.append("<select class='filter-select form-control' id='" + domid + "' >" +
						"<option value=''></option>" +
						"<option value='false'>否</option>" +
						"<option value='true'>是</option>" +
						"</select>");
				break;
			}
			case SELECTBOX: {
				condition.append("<option value='eq'>=</option>");
				condition.append("<option value='noteq'>&lt;&gt;</option>");
				condition.append("<option value='isnull'>为空</option>");
				condition.append("<option value='notnull'>不为空</option>");

				List<Map<String, Object>> selectList = exParam.getSelectList();

				FieldRecord field = exParam.getFieldRecord();

				inputHtml.append("<select id='" + domid + "' ");
				if ((null != field.getIfcascade() && field.getIfcascade() != 0)) {
					inputHtml.append(" onchange='chageEnumSelectBoxValue(this)'");
					FieldRecord resField = fieldRecordDao.getFieldInfo(field.getIfcascade());
					String restricterName = exParam.getTableName() + "__" + resField.getName();
					inputHtml.append(" restritname='" + restricterName + "'");
				}
				inputHtml.append(" class='filter-select form-control' >");

				if (exParam.isNeedBlank()) {
					inputHtml.append("<option value='' ></option>");
				}
				if (null != selectList && selectList.size() > 0) {
					for (Map<String, Object> selMap : selectList) {
						if (null != field.getRestricter() && field.getRestricter() != 0) {
							inputHtml.append("<option style='display:none;' resoptionid='" + selMap.get("resoptionid") + "' value='" + selMap.get("id") + "'>" + selMap.get("name") + "</option>");
						} else {
							inputHtml.append("<option value='" + selMap.get("id") + "'>" + selMap.get("name") + "</option>");
						}
					}
				}
				inputHtml.append("</select>");
				break;
			}
			case MUTIBOX: {
				condition.append("<option value='eq'>=</option>");
				condition.append("<option value='noteq'>&lt;&gt;</option>");
				condition.append("<option value='isnull'>为空</option>");
				condition.append("<option value='notnull'>不为空</option>");
				condition.append("<option value='include'>包含</option>");
				condition.append("<option value='notinclude'>不包含</option>");

				List<Map<String, Object>> muiboxList = exParam.getSelectList();
				inputHtml.append("<div class='mutiselectDiv'>");
				inputHtml.append("<input type='text' id='" + domid + "_osv' readonly='readonly' class='tb-mutiselect filter-select form-control' value=''/>");
				inputHtml.append("<span class='filter-mutiselect glyphicon glyphicon-triangle-bottom'></span>");
				inputHtml.append("<input id='" + domid + "' type='hidden' value='' name='" + domid + "'/>");
				if (null != muiboxList && muiboxList.size() > 0) {
					inputHtml.append("<div id='" + domid + "_div' class='mutiselect-value'>");
					for (Map<String, Object> selMap : muiboxList) {
						inputHtml.append("<label class='checkbox-label' checkLeable='" + domid + "' onclick='getMutiBoxCheckedValue(this)'>");
						inputHtml.append("<input type='checkbox' value='" + selMap.get("id") + "'/>");
						inputHtml.append("<span>" + selMap.get("name") + "</span>");
						inputHtml.append("</label>");
					}
					inputHtml.append("</div>");
				}

				inputHtml.append("</div>");
				break;
			}
			default: {
				break;
			}
		}
		condition.append("</select>").toString();
		inputHtml.toString();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("condition", condition);
		map.put("inputHtml", inputHtml);
		return map;
	}

	/**
	 * 获取实体所有布局字段
	 * @param entityId       实体id
	 * @param position       实体列表布局或过滤条件布局标识(List:列表布局，Falter：过滤条件布局)
	 * @return
	 */
	public List<FieldRecord> queryAllEntityLayoutField(Integer entityId, FieldPosition position)throws Exception{
		String showPostion = position == FieldPosition.List ? "list_show" : "filter_show";
		StringBuffer sql = new StringBuffer(" select id, name, inchinese from sys_custom_field where entity_id = ? and ");
		sql.append(showPostion).append(" = false ");
		return jdbcTemplate.query(sql.toString(),new Object[]{ entityId }, new FieldRecord.MainFieldRecord());
	}

	/**
	 * 获取用户自己设置的列表布局，过滤条件字段(xiaogq)
	 * @param employeeId        当前登录系统用户id
	 * @param entityId           实体id
	 * @param position           实体列表布局或过滤条件布局标识(List:列表布局，Falter：过滤条件布局)
	 * @return
	 */
	public List<FieldRecord> queryEmpSetEntityLayoutField(String employeeId,Integer entityId,FieldPosition position)throws Exception{
		StringBuffer sql = new StringBuffer("select DISTINCT b.id, b.name, b.inchinese, b.share_field_id, b.restricter,b.ifcascade ");
		sql.append(" from sys_custom_field_employee a left join sys_custom_field b on a.field_id=b.id ");
		sql.append(" where a.employee_id=? and a.entity_id=? and a.position=? and b.name != 'id' order by a.sequence ");
		List<FieldRecord> list = jdbcTemplate.query(sql.toString(),new Object[]{ employeeId, entityId, position.name() }, new FieldRecord.PartInfo());
		return list;
	}

	/**
	 * 获取管理员设置的列表布局，过滤条件字段(xiaogq)
	 * @param entityId        实体id
	 * @param position        实体列表布局或过滤条件布局标识(List:列表布局，Falter：过滤条件布局)
	 * @return
	 */
	public List<FieldRecord> querySysSetEntityLayoutField(Integer entityId,FieldPosition position)throws Exception{
		StringBuffer sql = new StringBuffer("select DISTINCT b.id, b.name, b.inchinese, b.share_field_id, b.restricter,b.ifcascade");
		sql.append(" from sys_org_field_employee a left join sys_custom_field b on a.field_id=b.id ");
		sql.append(" where a.entity_id=? and a.position=? and b.name != 'id' order by a.sequence ");
		List<FieldRecord> list = jdbcTemplate.query(sql.toString(),new Object[]{ entityId, position.name() }, new FieldRecord.PartInfo());
		return list;
	}

	/**
	 * 获取下拉框与单选多选数据
	 * @param entityId        实体id
	 * @param field            字段对象信息
	 * @return
	 */
	public List<Map<String, Object>> queryEntityFieldSelectBox(Integer entityId, FieldRecord field)throws Exception{
		StringBuffer sql = new StringBuffer();
		//作为查询条件的字段名name
		String fieldName = "";
		if(field != null){
			fieldName = field.getName();
			if(null != field.getRestricter() && field.getRestricter() != 0){//如果是下拉联动字段
				//如果当前字段共享其他字段处理
				if(null != field.getShareFieldId() && field.getShareFieldId() != 0){
					sql.append("select sf.res_option_id as resoptionid,s.id,s.name from sys_custom_selectbox s left JOIN  sys_custom_field_restrict sf ");
					sql.append(" on s.id = sf.option_id  LEFT JOIN sys_custom_field f on sf.field_id = f.share_field_id ");
					sql.append(" where f.entity_id = "+entityId+" and f.id = "+field.getId());
					sql.append(" order by s.position ");
				}else{
					sql.append("select sf.res_option_id as resoptionid,s.id,s.name from sys_custom_selectbox s left JOIN  sys_custom_field_restrict sf ");
					sql.append(" on s.id = sf.option_id  LEFT JOIN sys_custom_field f on sf.field_id = f.id ");
					sql.append(" where f.entity_id = "+entityId+" and f.id = "+field.getId());
					sql.append(" order by s.position ");
				}
			}else{
				//如果当前字段共享其他字段处理
				if(null != field.getShareFieldId() && field.getShareFieldId() != 0){
					sql.append("select s.id,s.name from sys_custom_selectbox s join sys_custom_field f on s.field_id = f.share_field_id ");
					sql.append("where f.name = '"+fieldName+"' and f.entity_id = " + entityId + " order by s.position");
				}else{
					sql.append("select s.id,s.name from sys_custom_selectbox s join sys_custom_field f on s.field_id = f.id ");
					sql.append("where f.name = '"+fieldName+"' and f.entity_id = " + entityId + " order by s.position");
				}
			}
		}
		//System.out.println("sql=="+sql.toString());
		return jdbcTemplate.queryForList(sql.toString());
	}

	/**
	 * 保存布局、或者过滤条件信息(xiaoxiumei)
	 * @param employeeId       当前登录系统用户Id
	 * @param entityId         实体Id
	 * @param position         列表布局或过滤条件字段标识（List:列表布局，Filter:过滤条件布局）
	 * @param fieldList        设置的字段信息
	 * @param isDefault        是系统布局还是自定义布局标识(true:系统布局，false：自定义布局)
	 */
	public int saveFieldEmployeeLayout(String employeeId,Integer entityId,FieldPosition position,List<FieldRecord> fieldList,boolean isDefault)throws Exception{
		int [] lens = new int[0];
		String[] sqlList = new String[fieldList.size()+1];
		// 删除现有显示字段
		StringBuilder delSql = new StringBuilder();
		if(isDefault){//如果是系统布局
			delSql.append(" delete from sys_org_field_employee where entity_id = "+ entityId +" and position = '" + position + "' ");
		}else{//如果是自己布局
			delSql.append(" delete from sys_custom_field_employee where entity_id = "+ entityId +" and position = '" + position + "' ");
			delSql.append(" and employee_id = '" + employeeId + "' ");
		}
		sqlList[0] = delSql.toString();
		// 设置显示字段
		if( null != fieldList && fieldList.size() > 0 ){
			for (int i = 0; i < fieldList.size(); i++) {
				StringBuilder insSql = new StringBuilder();
				if(isDefault){//如果是系统布局
					insSql.append("insert into sys_org_field_employee(entity_id, field_id, position, sequence) values ");
					insSql.append(" ( "+ entityId +", "+ fieldList.get(i).getId() +", '"+ position.name()+"', " + (i+1) +")");
				}else{//如果是自己布局
					insSql.append("insert into sys_custom_field_employee(entity_id, field_id, position, sequence, employee_id) values ");
					insSql.append(" ( "+ entityId +", "+ fieldList.get(i).getId() +", '"+ position.name()+"', " + (i+1) +",'"+employeeId+"')");
				}
				sqlList[i+1] = insSql.toString();
			}
		}
		if (sqlList.length > 0) {
			lens = jdbcTemplate.batchUpdate(sqlList);
		}
		return lens.length;
	}

	public Map<String, Object> getEntityColumns(Employee emp, Integer entityId,Boolean showOperation,Boolean showCheckbox) throws Exception{
		//是否显示停用启用表格列头(true:显示,false:隐藏)
		boolean showFlag = true;
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject fileMap = new JSONObject();
		//存放bootstrap列头
		StringBuffer columns = new StringBuffer();
		//存放实体数据库字段
		StringBuffer fields = new StringBuffer();
		//获取实体已经布局字段信息
		List<FieldRecord> fieldsList = this.queryEntityLayoutField(emp,entityId, FieldPosition.List);
		if (fieldsList == null){
			fieldsList = new ArrayList<FieldRecord>();
		}
		//设置id字段
		FieldRecord idField = new FieldRecord();
		idField.setName("id");
		idField.setInchinese("id");
		if (!fieldsList.contains(idField)) {//如果id没有在布局内则添加字段id
			fieldsList.add(0,idField);
		}
		//设置停用/启用字段
		FieldRecord flagField = new FieldRecord();
		flagField.setName("flag_bit");
		flagField.setInchinese("停用标记");
		if (!fieldsList.contains(flagField)) {//如果flag_bit没有在布局内则添加字段flag_bit
			showFlag = false;
			fieldsList.add(flagField);
		}
		if (fieldsList != null && fieldsList.size() > 0) {
			if(showCheckbox){
				columns.append("<th data-field='position' data-checkbox='true'></th>");
			}
			if (showOperation) {
				columns.append("<th data-field='operation' data-formatter='showColOperator'><div style='width:80px;'>操作</div></th>");
			}
			for (int i = 0; i < fieldsList.size(); i++) {
				FieldRecord field = fieldsList.get(i);
				fields.append(field.getName()).append(",");
				fileMap.put(field.getName(),field.getInchinese());
				if(field.getName().equals("id")){
					columns.append("<th data-field='"+field.getName()+"' data-visible='false'>"+field.getInchinese()+"</th>");
				}else if(field.getName().equals("flag_bit")){//如果是停用/启用表示字段
					columns.append("<th data-field='"+field.getName()+"' data-visible='"+showFlag+"' data-sortable='true'>"+field.getInchinese()+"</th>");
				}else if(field.getName().endsWith("_textarea") || field.getName().endsWith("_textblock") || (field.getName().endsWith("_varchar") && !field.getName().equals("name_varchar"))){
					columns.append("<th data-field='"+field.getName()+"' data-formatter='dataFormatter' data-sortable='true'>"+field.getInchinese()+"</th>");
				}else{
					columns.append("<th data-field='"+field.getName()+"' data-sortable='true'>"+field.getInchinese()+"</th>");
				}
			}
		}
		fields.deleteCharAt(fields.lastIndexOf(","));
		map.put("fields", fields);
		map.put("columns", columns);
		map.put("fileMap",fileMap);
		return map;
	}
}
